package com.net.xpay.clean.scheduler;


import com.google.common.base.Preconditions;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.common.domain.stat.CompanyBillDailyStat;
import com.net.xpay.common.manager.stat.CompanyBillDailyStatManager;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import com.net.xpay.core.model.SafePayTransBankDailyStatModel;
import com.net.shard.ShardDataSource;
import com.net.shard.ShardDataSourceConfigurer;
import com.net.shard.ShardUtil;
import com.net.shard.domain.ShardConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;

import javax.sql.DataSource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 2019/7/15 16:28

 * 新增统计字段
 */
@Slf4j
//@Component
public class CompanyBillStatAddColumnDataScheduler {



    @Autowired
    private SafePayBatchManager safePayBatchManager;


    @Autowired
    private CompanyBillDailyStatManager companyBillDailyStatManager;

    @Autowired
    @Qualifier("shardDataSource")
    private DataSource shardDataSource;


    @Scheduled(cron = "0 11 21 * * ?")
    public void updateCompanyDailyBillStat() {
        log.info("updateCompanyDailyBillStat start");
        //查出所有的 for循环处理单条更新
        List<CompanyBillDailyStat> list = companyBillDailyStatManager.listByCondition(new CompanyBillDailyStat(), new ArrayList<>());
        for (CompanyBillDailyStat stat : list) {
            BigDecimal bankRatio = new BigDecimal("0.0005");
            if (stat.getDay().isBefore(LocalDate.of(2024, 2, 6))) {
                bankRatio = new BigDecimal("0.001");
            }

            BigDecimal estimateChargeCost = new BigDecimal(stat.getRechargeAmount()).multiply(new BigDecimal("0.01")).multiply(new BigDecimal("0.006")).setScale(2, RoundingMode.HALF_UP);
            BigDecimal estimateBankCost = stat.getSafePayTransSuccessBankAmount().multiply(bankRatio).setScale(2, RoundingMode.HALF_UP);
            stat.setEstimateChargeCost(estimateChargeCost);
            stat.setEstimateBankCost(estimateBankCost);
            companyBillDailyStatManager.update(stat);
        }
        log.info("updateCompanyDailyBillStat end");
    }

    private void  statCompanyDailyBillStat(CompanyBillDailyStat stat) {
        ShardDataSourceConfigurer shardDataSourceConfigurer = ((ShardDataSource) shardDataSource).getShardDataSourceConfigurer();
        Map<Long, ShardConfig> shardConfigMap = shardDataSourceConfigurer.getShardConfigMap();
        Preconditions.checkArgument(CollectionUtils.isNotEmpty(shardConfigMap.keySet()));
        LocalDate day = stat.getDay();
        // 获取当天的起始时间和终止时间 终止时间保留在23:59:59.999999999
        LocalDateTime minEndTime = LocalDateUtil.getDayStart(day);
        LocalDateTime maxEndTime = LocalDateUtil.getDayStart(day.plusDays(1));
        //分库
        for (Long shardId : shardConfigMap.keySet()) {
            try {
                ShardUtil.setShardContext(shardId);
                log.info("statCompanyDailyBillStat start in shard={}|day={}", shardId, day);

                //统计数据
                //安全发到卡转账金额 复用了统计代码 这里的结束时间单独设置
                SafePayTransBankDailyStatModel safePayTransBankDailyStatModel = safePayBatchManager.statSafePayTransBankAmount(minEndTime, LocalDateUtil.getDayEnd(day));
                stat.setSafePayTransSuccessBankAmount(stat.getSafePayTransSuccessBankAmount().add(safePayTransBankDailyStatModel.getSafePayTransSuccessBankAmount()));


                log.info("statCompanyDailyBillStat end in shard={}|day={}", shardId, day);
            } finally {
                ShardUtil.cleanShardContext();
            }
        }
        companyBillDailyStatManager.update(stat);
    }


}
